package scan.controller;

import javafx.application.Application;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scan.mapper.ShopMapper;
import scan.po.OrderPO;
import scan.thread.MyThread;
import scan.util.IDWorker;

import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

@RestController
public class TestController {
    @Autowired
    private ShopMapper shopMapper;

    @RequestMapping("test")
    public void test() throws UnknownHostException, InterruptedException {
        List<OrderPO> orderPOList=new ArrayList<OrderPO>();
        Date date=new Date();
        Random r = new Random();
        for(int i=0;i<1000000;i++)
        {
            OrderPO orderPO=new OrderPO();
            orderPO.setOrderId(IDWorker.getInstance("eshop_order").nextId());
            orderPO.setCreateUserId(orderPO.getOrderId());
            orderPO.setModifyUserId(orderPO.getOrderId());
            orderPO.setStatus(Short.valueOf(String.valueOf(r.nextInt(4)+1)));
            orderPO.setTotalPrice(BigDecimal.valueOf(0));
            orderPO.setCreateTime(date);
            orderPO.setDealTime(date);
            orderPO.setModifyTime(date);
            orderPO.setShipTime(date);
            orderPO.setPaymentTime(date);
            orderPOList.add(orderPO);
        }
        List<OrderPO> templist=new ArrayList<OrderPO>();
        int j = 0;
        final CountDownLatch endLatch = new CountDownLatch(99);
        System.out.println("开始");
        Long startime=System.currentTimeMillis();
        for (int i = 0; i < orderPOList.size(); i++, j++) {
            if (j > 10000) {
                new MyThread(shopMapper,templist,endLatch).start();
                templist = new ArrayList<OrderPO>();
                templist.add(orderPOList.get(i));
                j = 0;
            } else {
                templist.add(orderPOList.get(i));
            }
        }
        if (templist.size() != 0) {
            shopMapper.insert(templist);
        }
        endLatch.await();
        Long endtime=System.currentTimeMillis();
        System.out.println("done:"+(endtime-startime));
    }
}
